package com.yoomiito.app.present.my.fragment;

import android.widget.Toast;

import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseLazyFragmentPresenter;
import com.yoomiito.app.model.CreditCard;
import com.yoomiito.app.model.bean.UserInfo;
import com.yoomiito.app.model.bean.WalletUrl;
import com.yoomiito.app.model.my.MyUserCenterInfo;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.ui.fragment.my.MyFragment;
import com.yoomiito.app.utils.NetworkUtils;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.XApi;

public class MyFmPresenter extends BaseLazyFragmentPresenter<MyFragment> {

    public MyFmPresenter(AppConfig appConfig) {
        super(appConfig);
    }

    public void getUserCenterData(){
        YmApi.getYmService()
                .getUserCenterInfo()
                .compose(getV().bindToLifecycle())
                .compose(XApi.<MyUserCenterInfo>getApiTransformerData())
                .compose(XApi.<MyUserCenterInfo>getScheduler())
                .subscribe(new ApiSubscriber<MyUserCenterInfo>(handleException()) {
                    @Override
                    public void onSuccess(MyUserCenterInfo userInfo) {
                        getV().setData(userInfo);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        Toast.makeText(getV().getActivity(),"error",Toast.LENGTH_SHORT).show();
                    }
                });
    }


    public void getMyPublishData(){
        YmApi.getYmService()
                .getUserCenterInfo()
                .compose(getV().bindToLifecycle())
                .compose(XApi.<MyUserCenterInfo>getApiTransformerData())
                .compose(XApi.<MyUserCenterInfo>getScheduler())
                .subscribe(new ApiSubscriber<MyUserCenterInfo>() {
                    @Override
                    public void onSuccess(MyUserCenterInfo userInfo) {
                        getV().setData(userInfo);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        Toast.makeText(getV().getActivity(),"error",Toast.LENGTH_SHORT).show();
                    }
                });
    }

    public void getUserInfo() {
        YmApi.getYmService()
                .getUserInfo()
                .compose(getV().bindToLifecycle())
                .compose(XApi.<UserInfo>getApiTransformerData())
                .compose(XApi.<UserInfo>getScheduler())
                .subscribe(new ApiSubscriber<UserInfo>(handleException()) {
                    @Override
                    public void onSuccess(UserInfo userInfo) {
                        getV().setUserInfo(userInfo);
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }


    public void getWalletUrl() {
        YmApi.getYmService()
                .getWalletUrl()
                .compose(getV().bindToLifecycle())
                .compose(XApi.<WalletUrl>getApiTransformerData())
                .compose(XApi.<WalletUrl>getScheduler())
                .subscribe(new ApiSubscriber<WalletUrl>(handleException()) {
                    @Override
                    public void onSuccess(WalletUrl userInfo) {
                        getV().jumpUrl(userInfo);
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }


    public void creditCardUrl(){
        YmApi.getYmService()
                .creditCardUrl()
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<CreditCard>(handleException()) {
                    @Override
                    public void onSuccess(CreditCard creditCard) {
                        getV().setCreditCard(creditCard);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                    }
                });

    }
}
